#! /usr/bin/env python

# this script used to convert bed file format into cod format

"""
bed format like:
#chromosome	start	end	[name]

cod format like:
#seq_id	chromosome	start	end	strand

"""

def bed2cod(file1,file2):
	f = open(file1,'r')
	f2 = open(file2,'w')
	print >>f2, "#seq_id	chromosome	start	end	strand"
	lines = f.readlines()
	for i,line in enumerate(lines):
		temp= line.split()
		linelen = len(temp)
		if linelen == 3:
			print >>f2,'\t'.join([repr(i+1), temp[0], temp[1],temp[2],'+'])
		if linelen >3:
			print >>f2, '\t'.join([temp[3], temp[0], temp[1], temp[2],'+'])
	f.close()
	f2.close()
	
if __name__ == '__main__': 
	import sys
	import time
	
	if len(sys.argv) == 1:
		print "usage:python bed2cod.py bedfile codfile"
	else:
		bedfile = sys.argv[1]
		codfile = sys.argv[2]
		starttime = time.time()
		bed2cod(bedfile,codfile)
		endtime = time.time()
		print "DONE! Takes %f seconds" % (endtime-starttime)
